var main = {
    id : 0 ,
    ai : null ,
    shop : null ,
    user : null ,
    originSrc : '' ,
    ls : window.localStorage ,
    search : urlParse() ,
    img : $('#photo-image') ,
    effImgs : $$('.effects-in img') ,
    curEffImg : $$('.effects-in img')[0] ,
    switchEff : function () {
        var self = this;
        for (var i = 0 ; i < self.effImgs.length ; i++) {
            self.effImgs[i].onclick = function () {
                self.curEffImg.removeClass('selected');
                self.curEffImg = this;
                self.curEffImg.addClass('selected');
                self.img.src = self.originSrc;
                var that = this;
                setTimeout(function () {
                    self.ai = AlloyImage(self.img);
                    var eff = that.getAttribute('eff');
                    if (eff == '原图') {
                        return;
                    }
                    self.ai.ps(eff).replace(self.img);
                } , 50);
            }
        }
    } ,
    init : function () {
        var self = this;
        self.switchEff();
        self.user = JSON.parse(self.ls.getItem('user'));
        self.shop = JSON.parse(self.ls.getItem('shop'));
        $('#take-photo').addEventListener('touchstart' , function () {
            main.takePhoto();
        });
        $('#take-aggin').addEventListener('touchstart' , function () {
            main.takeAgain();
        });
        $('#save-photo').addEventListener('touchstart' , function () {
            main.savePhoto();
        });
        $('#not-share').addEventListener('touchstart' , function () {
            main.notSharePhoto();
        });
        $('#agree-share').addEventListener('touchstart' , function () {
            main.sharePhoto();
        });
        $('.backstep').href = "index.html" + window.location.search;
    } ,
    takePhoto : function () {
        var self = this;
        $('#take-photo').innerText = "正在加载照片...";
        ajax.get({
            url : '/snapshot/user/takePhoto.do' ,
            success : function (resp) {
                if (resp.success) {
                    self.originSrc = self.img.src = "data:image/jpeg;base64," + resp.value;
                    $('.ps-page').removeClass('none');
                    $('.main-page').addClass('none');
                } else {
                }
            } ,
            error : function (resp) {
                alert(resp.message);
            }
        });
    } ,
    savePhoto : function () {
        if ($("#save-photo").innerText == "保存成功!")
            return;
        $("#save-photo").innerText = "保存中...";
        var self = this , base64 = self.img.src.replace("data:image/png;base64," , '').replace("data:image/jpeg;base64," , "").replace(/\+/gi , "%2B");
        ajax.post({
            url : '/snapshot/user/savePhoto.do' ,
            data : {
                base64Photo : base64
            } ,
            success : function (resp) {
                if (resp.success) {
                    $("#save-photo").innerText = "保存成功!";
                    setTimeout(function () {
                        $('.share-message').removeClass('none');
                        $('.share-message-text').innerText = self.shop.name + "店长向您申请在店内分享您的照片，可以吗?";
                    } , 200);
                    self.id = resp.value.id;
                } else {
                    alert(resp.message);
                    $("#save-photo").innerText = "保存";
                }
            }
        });
    } ,
    takeAgain : function () {
        $('.main-page').removeClass('none');
        $('.ps-page').addClass('none');
        $('#take-photo').innerText = "对着镜子，点我拍照~_~";
    } ,
    sharePhoto : function () {
        var self = this;
        $('#agree-share').innerText = "感谢您的分享";
        $('.share-message-text').innerTex = "成功保存并分享!" + self.shop.name + "店长感谢您的分享，祝您购物愉快!";
        ajax.get({
            url : '/snapshot/user/updateUserImages.do' ,
            data : {
                state : 1 ,
                imageId : this.id
            }
        });
        setTimeout(function () {
            $('.share-message').addClass('none');
            $('.main-page').removeClass('none');
            $('.ps-page').addClass('none');
            $("#save-photo").innerText = "保存";
            $('#agree-share').innerText = "好吧~_~";
            $('#take-photo').innerText = "对着镜子，点我拍照~_~";
        } , 1000);
    } ,
    notSharePhoto : function () {
        var self = this;
        $('#agree-share').innerText = "感谢您的使用";
        $('.share-message-text').innerText = "成功保存!" + self.shop.name + "店长祝您购物愉快!";
        ajax.get({
            url : '/snapshot/user/updateUserImages.do' ,
            data : {
                state : 2 ,
                imageId : this.id
            }
        });
        setTimeout(function () {
            $('.share-message').addClass('none');
            $('.main-page').removeClass('none');
            $('.ps-page').addClass('none');
            $("#save-photo").innerText = "保存";
            $('#agree-share').innerText = "暂不分享";
            $('#take-photo').innerText = "对着镜子，点我拍照~_~";
        } , 1000);
    }
}

window.addEventListener("DOMContentLoaded" , function () {
    main.init();
} , false);